// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   FavoriteServiceImpl.java

package com.itheima.travel.service.impl;

import com.itheima.travel.dao.IFavoriteDao;
import com.itheima.travel.model.*;
import com.itheima.travel.service.IFavoriteService;
import com.itheima.travel.util.FactoryUtil;
import com.itheima.travel.util.JdbcUtils;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.sql.DataSource;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.transaction.support.TransactionSynchronizationManager;

public class FavoriteServiceImpl
	implements IFavoriteService
{

	private IFavoriteDao favoriteDao;

	public FavoriteServiceImpl()
	{
		favoriteDao = (IFavoriteDao)FactoryUtil.getInstance("IFavoriteDao");
	}

	public PageBean getPageBean(int curPage, int uid)
		throws Exception
	{
		PageBean pageBean = new PageBean();
		pageBean.setCurPage(curPage);
		int pageSize = 4;
		pageBean.setPageSize(pageSize);
		int count = favoriteDao.getCountByUid(uid);
		pageBean.setCount(count);
		List mapList = favoriteDao.findFavoriteListByPage(uid, curPage, pageSize);
		List favoriteList = null;
		if (mapList != null && mapList.size() > 0)
		{
			favoriteList = new ArrayList();
			Favorite favorite;
			for (Iterator iterator = mapList.iterator(); iterator.hasNext(); favoriteList.add(favorite))
			{
				Map map = (Map)iterator.next();
				Route route = new Route();
				favorite = new Favorite();
				BeanUtils.populate(favorite, map);
				BeanUtils.populate(route, map);
				favorite.setRoute(route);
			}

		}
		pageBean.setData(favoriteList);
		return pageBean;
	}

	public boolean isFavoriteByRidAndUserId(int rid, int uid)
		throws Exception
	{
		Favorite favorite = favoriteDao.findFavoriteByRidAndUserId(rid, uid);
		return favorite != null;
	}

	public void addFavorite(int rid, User user)
		throws Exception
	{
		Favorite favorite;
		JdbcTemplate jdbcTemplate;
		Connection connection;
		favorite = new Favorite();
		Route route = new Route();
		route.setRid(rid);
		favorite.setRoute(route);
		favorite.setUser(user);
		favorite.setDate((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()));
		DataSource dataSource = JdbcUtils.getDataSource();
		jdbcTemplate = new JdbcTemplate(dataSource);
		TransactionSynchronizationManager.initSynchronization();
		connection = DataSourceUtils.getConnection(dataSource);
		connection.setAutoCommit(false);
		int row = favoriteDao.addFavorite(jdbcTemplate, favorite);
		row = favoriteDao.updateRouteFavoriteNum(jdbcTemplate, rid);
		connection.commit();
		SQLException e;
		try
		{
			TransactionSynchronizationManager.clearSynchronization();
			connection.setAutoCommit(true);
		}
		// Misplaced declaration of an exception variable
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		break MISSING_BLOCK_LABEL_214;
		e;
		e.printStackTrace();
		connection.rollback();
		try
		{
			TransactionSynchronizationManager.clearSynchronization();
			connection.setAutoCommit(true);
		}
		// Misplaced declaration of an exception variable
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		break MISSING_BLOCK_LABEL_214;
		Exception exception;
		exception;
		try
		{
			TransactionSynchronizationManager.clearSynchronization();
			connection.setAutoCommit(true);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		throw exception;
	}
}
